


package yf.mall.controller.app;

import com.yf.request.SearchParam;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.yf.response.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.AppCategoryEntity;
import yf.mall.enums.EnumVO;
import yf.mall.enums.LevelEnum;
import yf.mall.enums.app.AppCourseHotEnum;
import yf.mall.service.app.AppCategoryService;
import yf.mall.vo.AppCourseVo;
import yf.mall.service.app.AppCourseService;
import com.yf.response.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/app/course")
public class AppCourseController {
    @Autowired
    private AppCourseService appCourseService;

    @RequestMapping("/list")
    @RequiresPermissions("app:course:list")
    public R list(@RequestBody SearchParam<AppCourseVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<AppCourseVo> list = appCourseService.findVo(params.getEntity());
        return R.ok(new PageInfo(list));
    }

    @RequestMapping("/down")
    @RequiresPermissions("app:course:list")
    public void down(@RequestBody SearchParam<AppCourseVo> params, HttpServletResponse response) {
        List<AppCourseVo> list = appCourseService.findVo(params.getEntity());
        appCourseService.downLoad(response, list);
    }

    @RequestMapping("/upload")
    public void upload(MultipartFile file) {
        try {
            appCourseService.upload(file.getInputStream());
        } catch (IOException e) {
            throw new RRException("上传的文件有误!");
        }
    }


    @RequestMapping("/info/{id}")
    @RequiresPermissions("app:course:info")
    public R info(@PathVariable("id") Long id) {
        AppCourseVo vo = appCourseService.getVo(id);

        return R.ok(vo);
    }

    @RequestMapping("/save")
    @RequiresPermissions("app:course:save")
    public R save(@RequestBody AppCourseVo vo) {
        appCourseService.saveVo(vo);
        return R.ok(vo);
    }

    @RequestMapping("/update")
    @RequiresPermissions("app:course:update")
    public R update(@RequestBody AppCourseVo vo) {
        appCourseService.updateVo(vo);

        return R.ok(vo);
    }

    @RequestMapping("/delete")
    @RequiresPermissions("app:course:delete")
    public R delete(Long[] ids) {
        appCourseService.deleteBatch(ids);

        return R.ok();
    }

    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order != null ? order : "id desc");
        List<AppCourseVo> list = appCourseService.findVo(null);
        return R.ok(list);
    }

    @Resource
    private AppCategoryService categoryService;

    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String, Object> options = new HashMap();
        options.put("hotOptions", AppCourseHotEnum.names());
        //课程分类 Options最好是value Map
        List<AppCategoryEntity> courseOptions = categoryService.findForCourse();
        options.put("courseOptions", courseOptions.stream().collect(Collectors.toMap(AppCategoryEntity::getType,Function.identity())));
        List<EnumVO> levelOptions =  Arrays.stream(LevelEnum.values()).map(item->new EnumVO(item.name,item.levelId.intValue())).collect(Collectors.toList());
        options.put("levelOptions",levelOptions.stream().collect(Collectors.toMap(EnumVO::getValue, Function.identity())));
        return R.ok(options);
    }


    /***********自定义开始***********/
}
